package com.hoyo.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hoyo.common.PageInfo;
import com.hoyo.domain.Bullet;
import com.hoyo.domain.Comment;
import com.hoyo.service.MovieService;

@Controller
public class MovieController {
	
	@Autowired
	private MovieService movieService;
	
	@RequestMapping("/movie/{movieId}/url")
	@ResponseBody
	public String playMovie(@PathVariable("movieId") int movieId){
		try {
			return movieService.playMovie(movieId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/*
	 * 获取弹幕
	 */
	@RequestMapping("/movie/bullets/{movieId}")
	@ResponseBody
	public String bullets(@PathVariable("movieId") int movieId){
		
		JSONObject obj = new JSONObject();
		try {
			List<Bullet> bullets = movieService.findBulletsByMovieIdOnMoviePlayed(movieId);
			obj.put("code", 1);
			obj.put("danmuku", bullets);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("code", 0);
		}
		return obj.toString();
	}
	
	
	/*
	 * 获取某个视频的某页评论
	 */
	@RequestMapping("/movie/comments")
	@ResponseBody
	public String comments(@RequestParam("movieId") int movieId, @RequestParam("pageNum") int pageNum){
		
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Comment> pageInfo = movieService.pageFindCommentsByMovieId(movieId, pageNum);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
			obj.put("errorMsg", e.getMessage());
		}
		return obj.toString();
	}
	
	/*
	 * 获取某个视频的某页的热门评论
	 */
	@RequestMapping("/movie/topComments")
	@ResponseBody
	public String topComments(@RequestParam("movieId") int movieId, @RequestParam("pageNum") int pageNum){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Comment> pageInfo = movieService.pageFindTopCommentsByMovieId(movieId, pageNum);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
			obj.put("errorMsg", e.getMessage());
		}
		return obj.toString();
	}
	
	@RequestMapping("/movie/cmts")
	@ResponseBody
	public String commentsOnMoviePlaying(@RequestParam("movieId") int movieId){
		
		JSONObject obj = null;
		try {
			obj = movieService.commentsOnMoviePlaying(movieId);
			
		} catch (Exception e) {
			e.printStackTrace();
			obj = new JSONObject();
			obj.put("success", false);
			obj.put("errorMsg", e.getMessage());
		}
		return obj.toString();
	}
	
	@RequestMapping("/movie/url")
	@ResponseBody
	public String url(@RequestParam("id") int id){
		
		JSONObject obj = new JSONObject();
		try {
			String url = movieService.translateId2url(id);
			obj.put("success", true);
			obj.put("url", url);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	
	/*
	 * 转变播放视频时调用，获取对应的视频url token和对应视频下的评论
	 */
	@RequestMapping("/movie/{movieId}")
	@ResponseBody
	public String changeMovie(@PathVariable("movieId") int movieId){
		
		JSONObject obj = null;
		try {
			obj = movieService.commentsOnMoviePlaying(movieId);
			obj.put("url", movieService.playMovie(movieId));
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj = new JSONObject();
			obj.put("success", false);
			obj.put("errorMsg", e.getMessage());
		}
		return obj.toString();
	}
	
}










